/**
 * author      : dormi330
 * date        : 2018/7/5
 * project     : mybatis
 * description : TODO
 */

package com.deep.trade.adaptor.provided.rest;

import com.deep.trade.common.HttpResonsePayload;
import com.deep.trade.common.TradeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {

    private Logger logger = LoggerFactory.getLogger(this.getClass().getName());

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public HttpResonsePayload handler(Exception ex) {
        ex.printStackTrace();
        logger.error(ex.toString());
        String errMsg = ex instanceof TradeException ? ex.getMessage() : "some thing bad happen";
        return HttpResonsePayload.unhappy("1", errMsg);
    }
}
